<?php
/**
 * Created by PhpStorm.
 * User: DaibiDaddy
 * Date: 2016/5/24
 * Time: 17:14
 */

namespace App\Base;

use App\BaseExt\Pagination;
use App\Model\AdBannerModel;
use App\Model\LazyCommon\AdminLogModel;
use App\Model\LazyCommon\CCM;
use App\Model\LazyCommon\RulesModel;
use App\Model\LinksModel;
use App\Model\ProvinceModel;
use Symfony\Component\HttpFoundation\Request;

class BaseController4Web extends BaseController {

    public $data;

    public function initData()
    {
        $this->data["title"] = "政华教育";
        $this->data["keywords"] = "政华,政华教育,教育";
        $this->data["description"] = "政华教育政华教育政华教育政华教育政华教育政华教育";
        $this->data["rnd"] = "?t=".time();
        $this->data["rnd"] = "?t=v1.0";
        $this->data["headTab"] = "index";


        //获取当前省份
        if (!$province = $this->getParamRequired("province")) {$province = 0;}
        $this->data["province"] = $province;
        $this->data["provinceId"] = 0;
        //栏目ID
        $this->data["id_ms"] = BaseConfig::__CAT_MIANSHI__;
        $this->data["id_sz"] = BaseConfig::__CAT_SHIZHENG__;
        $this->data["id_ga"] = BaseConfig::__CAT_GONGAN__;
        $this->data["id_xc"] = BaseConfig::__CAT_XINGCE__;
        $this->data["id_sl"] = BaseConfig::__CAT_SHENLUN__;
        $this->data["id_kc"] = BaseConfig::__CAT_KECHENG__;
        $this->data["id_xscs"] = BaseConfig::__CAT_XIANSHNGCESHI__;
        $this->data["id_gajc"] = BaseConfig::__CAT_GONGANJICHU__;
        $this->data["id_stxz"] = BaseConfig::__CAT_SHITIXIAZAI__;
        $this->data["id_yxbys"] = BaseConfig::__CAT_YOUXIUBIYESHENG__;
        $this->data["id_xxhd"] = BaseConfig::__CAT_XIANXIAHUODONG__;
        $this->data["id_tjhd"] = BaseConfig::__CAT_TUANJIANHUODONG__;
        $this->data["id_xydt"] = BaseConfig::__CAT_XUEYUANDONGTAI__;

        $this->data["id_kszx"] = BaseConfig::__CAT_KAOSHIZIXUN__;
        $this->data["id_ksgg"] = BaseConfig::__CAT_KAOSHIGONGGAO__;
        $this->data["id_cjcx"] = BaseConfig::__CAT_CHENGJICHAXUN__;
        $this->data["id_msgg"] = BaseConfig::__CAT_MIANSHIGONGGAO__;

        $this->data["ids_pic"] = BaseConfig::__CAT_PIC_ART__;

        //获取链接
        $LinksModel = new LinksModel($this->getDoctrine());
        $links = $LinksModel->getRep()->findBy(array(),array("orders"=>"ASC"));
        $this->data["links"] = $links;

        //获取省份配置
        $bc = new BaseCache();
        $ProvinceList = $bc->getItem("province");
        $ProvinceList  = "";//暂时不用缓存了
        if (!$ProvinceList) {
            $ProvinceModel = new ProvinceModel($this->getDoctrine());
            $ProvinceList = $ProvinceModel->getAll();
            $bc->setItem("province",$ProvinceList);
        }
        $this->data["province"] = $ProvinceList;

        //获取网站配置信息
        $RulesModel = new RulesModel($this->getDoctrine());
        $webConf = [];
        $Rules = $RulesModel->getByC("2");
        foreach ($Rules as $one) {
            $webConf[$one->getK()] = $one->getV();
        }

        $this->data["conf"] = $webConf;

        //广告位
        $AdBannerModel = new AdBannerModel($this->getDoctrine());
        $adBannerData = $AdBannerModel->getAll();
        //--处理一下广告数据
        $adBannerArray = [];
        foreach ($adBannerData as $row) {
            $adBannerArray[$row->getPositionCode()] =  $row;
        }
        $this->data["adBannerArray"] = $adBannerArray;
    }


    /**
     * @param $base_url
     * @param $total_rows
     * @param int $per_page
     * @return array
     */
    public function creatPage($base_url,$total_rows,$per_page = 10)
    {
        $config['first_url'] = $base_url;
        $config['base_url'] = $base_url;
        $config['total_rows'] = $total_rows;
        $config['per_page'] = $per_page;
//        $config['reuse_query_string'] = TRUE;
//        $config["query_string_segment"] = "page";

        //分页基础配置
        $config['num_links'] = 3;
        $config['first_link'] = '首页';
        $config['prev_link'] = '« 上一页';
        $config['cur_tag_open'] = '<li class="active"><a>';
        $config['cur_tag_close'] = '</a></li>';
        $config['next_link'] = '下一页 »';
        $config['last_link'] = '末页';

        $pageBuilder = new Pagination($config);
        $page =  $pageBuilder->create_links();
        return array(
            "total"     => $total_rows,
            "perPage"   => $per_page,
            "page"      => $page
        );
    }

}
